<template>
  <div
    class="song-list-item"
    @click="myFirstAction"
    :class="{
      active: currentSong && currentSong.id === item.id,
      playing: currentSong && currentSong.id === item.id && playing,
    }"
  >
    <div class="checked-box" v-if="isShowPreCheckedbox">
      <van-checkbox v-model="checked" @change="handChange" />
    </div>
    <div class="left">
      <img :src="item.al ? item.al.picUrl : item.picUrl" alt="" />

      <div class="playicon">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>

    <div class="middle">
      <div class="top e_1">
        {{ item.name }}
        <span v-if="(item.song ? item.song.alias : item.alia).length">
          ({{ item.song ? item.song.alias[0] : item.alia[0] }})
        </span>
      </div>

      <div class="bottom e_1">
        <span
          v-for="ar in item.song ? item.song.artists : item.ar"
          :key="ar.id"
          >{{ ar.name }}</span
        >
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "SongListItem",
  props: {
    item: Object,
    isShowPreCheckedbox: Boolean,
  },

  computed: {
    ...mapState(["currentSong", "playing"]),

    isChecked() {
      return this.item.isChecked;
    },
  },

  data() {
    return {
      checked: false,
    };
  },

  methods: {
    // 更新歌曲选中状态属性
    handChange(flag) {
      this.$set(this.item, "isChecked", flag);
    },

    // 我的第一个actions，同时提交两个mutation
    myFirstAction() {
      // 如果点击了正在播放的就不分发
      if (this.item !== this.currentSong) {
        const action_name = "updateCurrentSongAndAddSongInPlayList";
        this.$store.dispatch(action_name, { song: this.item });
      }
      /*
        dispatch => 分发
        以载荷形式分发
        store.dispatch('incrementAsync', { amount: 10 })

        以对象形式分发
        store.dispatch({ type: 'incrementAsync', amount: 10 })
      */
    },
  },
};
</script>

<style lang="less" scoped>
.song-list-item {
  width: 100%;
  height: 50px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  transition: 0.2s;

  .left {
    width: 50px;
    height: 100%;
    overflow: hidden;

    img {
      border-radius: 10px;
    }
  }

  .middle {
    flex: 1;
    padding-left: 20px;

    .bottom {
      font-size: 12px;

      span {
        &::after {
          content: "/";
        }

        &:last-of-type {
          &::after {
            content: "";
          }
        }
      }
    }
  }

  &.active {
    color: skyblue;

    .left {
      position: relative;

      .playicon {
        display: flex;
        justify-content: space-around;
        width: 20px;
        height: 20px;
        position: absolute;
        top: 15px;
        left: 15px;
        z-index: 1;

        span {
          width: 15%;
          height: 100%;
          background-color: skyblue;
          display: inline-block;
          animation: loading 0.8s linear infinite alternate;
          transform-origin: bottom;
          animation-play-state: paused;

          &:nth-child(1) {
            animation-delay: -0.6s;
          }

          &:nth-child(2) {
            animation-delay: 0s;
          }

          &:nth-child(3) {
            animation-delay: -0.4s;
          }
        }
      }
    }

    &.playing {
      .left {
        .playicon {
          span {
            animation-play-state: running;
          }
        }
      }
    }
  }
}
</style>
